package io.github.annyylxyms.gyn.gateway;

import cn.gyn.gynrpc.core.api.RegistryCenter;
import cn.gyn.gynrpc.core.registry.gyn.GynRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;

import java.util.Properties;

import static io.github.annyylxyms.gyn.gateway.GatewayPlugin.GATEWAY_PREFIX;

/**
 * gateway config
 * @Configuration表示是一个配置类
 */
@Configuration
public class GatewayConfig {
    @Bean
    public RegistryCenter rc() {
        return new GynRegistryCenter();
    }

    @Bean
    ApplicationRunner runner(@Autowired ApplicationContext context) {
        //spring cloud gateway都是通过mapping找对应的filter做过滤的，老师这里不重新写mapping，而是在初始化的时候往已经存在的SimpleUrlHandlerMapping里加入一个filter
        //额外往里加了一个mapping，只要调了"/gw/**"的路径，就会走gatewayWebHandler
        return args -> {
            SimpleUrlHandlerMapping handlerMapping = context.getBean(SimpleUrlHandlerMapping.class);
            Properties mappings = new Properties();
            mappings.put(GATEWAY_PREFIX + "/**", "gatewayWebHandler");
            handlerMapping.setMappings(mappings);
            //setMappings后还要初始化handler才行
            handlerMapping.initApplicationContext();
            System.out.println("gynrpc gateway start");
        };

    }
}
